/*
Program: TicTacToe
Author: Michael Hoover
Date: 4-12-08
Objective:
Program a simple two player Tic Tac Toe game.
The game should take input from players, draw an 
ASCII game grid, and check for a winning condition 
after each play.  The game should determine if there 
are no winners as well.  The game should be a user 
created class.
*/

#include "stdafx.h"
#include<iostream>
using namespace std;

class TicTacToe
{
private:
	char grid[3][3];//2d array for gameplay
	char marker;
	char symbol;
	int count;
	int GameOver;
public:
	TicTacToe();
	void Show();
	void Play();
	void ErrorCheck(int, int);
	void Update(int, int);
	void CheckWin();
};

int _tmain(int argc, _TCHAR* argv[])
{
	cout << "++TIC-TAC-TOE++\n";

	TicTacToe game; 
    game.Play();
    return 0;
}

TicTacToe::TicTacToe()
{      
	//assign values to each array place
	for(int i = 0; i < 3; i++)
	{
		for(int j = 0; j < 3; j++)
		{
			if (i == 0 && j == 0)
				grid[i][j] = '1';
			else if (i == 0 && j == 1)
				grid[i][j] = '2';
			else if (i == 0 && j == 2)
				grid[i][j] = '3';
			else if (i == 1 && j == 0)
				grid[i][j] = '4';
			else if (i == 1 && j == 1)
				grid[i][j] = '5';
			else if (i == 1 && j == 2)
				grid[i][j] = '6';
			else if (i == 2 && j == 0)
				grid[i][j] = '7';
			else if (i == 2 && j == 1)
				grid[i][j] = '8';
			else if (i == 2 && j == 2)
				grid[i][j] = '9';
		}
	}
}

void TicTacToe::Play()
{
	int GameOver = 0;
	int count = 0;
    int selection;
    while (GameOver == 0)
    {
		Show();
        if (count % 2 == 0)
        {
			cout << "Player 1-choose your space: ";
			cin >> selection;
        }
        else
        {
			cout << "Player 2-choose your space: ";
			cin >> selection;
        }

        ErrorCheck(selection, count);
        Update(selection, count);
        CheckWin();
        count++;//update turns taken
     }
}

void TicTacToe::Show()
{      
	//print game grid
    cout << endl;
    cout << "   |   |   " << endl;
    cout << " " << grid[0][0] << " | " << grid [0][1] << " | " << grid [0][2] << endl;
    cout << "   |   |   " << endl;
    cout << "---+---+---" << endl;
    cout << "   |   |   " << endl;
    cout << " " << grid[1][0] << " | " << grid [1][1] << " | " << grid [1][2] << endl;
    cout << "   |   |   " << endl;
    cout << "---+---+---" << endl;
    cout << "   |   |   " << endl;
    cout << " " << grid[2][0] << " | " << grid [2][1] << " | " << grid [2][2] << endl;
    cout << "   |   |   " << endl;
    cout << endl;
}

void TicTacToe::ErrorCheck(int selection, int count)
{
	while (selection < 1 || selection > 9)//error check of player selection
    {
		cout << "Space already taken. Pick a different one: \n";
        cin >> selection;
    }
}

void TicTacToe::Update(int selection, int count)
{
	//decide marker based upon current player
    if (count % 2 == 0)
    {
		marker = 'X';
    }
    else
    {
        marker = 'O';
    }
    grid[(selection - 1) / 3][(selection - 1) % 3] = marker;
}

void TicTacToe::CheckWin()
{
	// verify win
    if (grid[0][0] == grid[0][1] && grid[0][2] == grid[0][0])
	{
		GameOver = 1;
		symbol = grid[0][0];
	}
    else if (grid[0][0] == grid[1][0] && grid[2][0] == grid[0][0])
	{
		GameOver = 1;
		symbol = grid[0][0];
	}
	else if (grid[0][0] == grid[1][1] && grid[2][2] == grid[0][0])
	{
		GameOver = 1;
		symbol = grid[0][0];
	}
	else if (grid[0][1] == grid[1][1] && grid[2][1] == grid[0][1])
	{
		GameOver = 1;
		symbol = grid[0][1];
	}
	else if (grid[0][2] == grid[1][1] && grid[2][0] == grid[0][2])
	{
		GameOver = 1;
		symbol = grid[0][2];
	}
	else if (grid[0][2] == grid[1][2] && grid[2][2] == grid[0][2])
	{
		GameOver = 1;
		symbol = grid[0][2];
	}
	else if (grid[1][0] == grid[1][1] && grid[1][2] == grid[1][0])
	{
		GameOver = 1;
		symbol = grid[1][0];
	}
	else if (grid[2][0] == grid[2][1] && grid[2][2] == grid[2][0])
	{
		GameOver = 1;
		symbol = grid[2][0];
	}

	//output decided winner
	if (GameOver == 1 && symbol == 'X')
	{
			Show();     
			cout << "Winner: Player 1\n";
			system("pause");
			exit(1);
	}
	else if (GameOver == 1 && symbol == 'O')
	{
		Show();
		cout << "Winner: Player 2\n";
		system("pause");
		exit(1);
	}
	if (count == 9)
		GameOver = 2;
	if (GameOver == 2)
	{
		Show();
		cout << "Tie...you both lose\n";
		system("pause");
		exit(1);
	}
}